using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLThuChi.Repositories
{
	#region using
	using QLThuChi.Models;
	#endregion
	public class BoPhanRepository
	{
		public static List<BoPhan> SelectAll()
		{
			return DataContext.Instance.BoPhans.OrderBy(item => item.TenBoPhan).ToList();
		}

		public static BoPhan SelectByID(int mabophan)
		{
			return DataContext.Instance.BoPhans.FirstOrDefault(item => item.MaBoPhan == mabophan );
		}

		public static bool Insert(BoPhan obj)
		{
			try
			{
				DataContext.Instance.BoPhans.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int mabophan)
		{
			try
			{
				var delitem = DataContext.Instance.BoPhans.FirstOrDefault(item => item.MaBoPhan == mabophan );
				DataContext.Instance.BoPhans.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<BoPhan> RetrieveByID(int mabophan)
		{
			return (from item in DataContext.Instance.BoPhans where  item.MaBoPhan == mabophan  select item).ToList();
		}

	}
}